import '../../scss/eleMsg.scss'
import Vue from 'vue'
import MintUI from 'mint-ui'
import com from '../unit/common'

Vue.use(MintUI)
window.onload = function () {
  let page = {
    _eleCode: com.getUrlParam('eleCode'),
    _app: {},
    _init() {
      this._loadTemp()
    },
    _loadTemp() {
      let self = this
      this._app = new Vue({
        el: '#app',
        created() {
          if( self._eleCode){
            this.elCode = self._eleCode
            this.getEleDetail(self._eleCode)
            this.getEleFaultHis()
          }else{
            alert('电梯数据异常')
          }
        },
        data() {
          return {
            elCode: '',
            barSelect: 0,
            headerMsg: {
              yearCheckDate: '...',
              nextMaintainDate: '...',
              lastMaintainDate: '...',
              entrance: '...',
              repairCount: '...',
              maintainCount: '...',
            },
            tabBar: [
              '维保记录',
              '维修记录',
              '巡检记录'
            ],
            eleLogs: [
              {
                type: '...',
                startDate: '...',
                person: '...',
                endDate: '...',
              },
            ],
            allLoaded: false,
            pageNo: 1,
          }
        },
        methods: {
          changeBar(index) {
            this.barSelect = index
            this.pageNo = 1
            this.eleLogs = []
            this.allLoaded = false
            this.getEleFaultHis()
          },
          // 获取电梯详情
          getEleDetail(code) {
            com.axio.post(`${com.basePath4}/elevator-web/elevatorLogs/elevatorDetail`, {
              registerCode: code
            }).then((data) => {
              this.headerMsg = data.data.data
            }).catch((err) => {
              console.log(err)
            })
          },
          // 获取电梯历史信息
          getEleFaultHis(cb) {
            com.axio.post(`${com.basePath4}/elevator-web/elevatorLogs/pageApp`, {
              pageNo: this.pageNo,
              pageSize: 6,
              condition: {
                elevatorCode: self._eleCode,
                type: this.barSelect + 1
              }
            }).then((data) => {
              const eleLog = data.data.data.data.map((v) => {
                v.startDate = com.formatDate(Number(v.startDate))
                v.endDate = com.formatDate(Number(v.endDate))
                return v
              })
              if (this.pageNo === 1) {
                this.eleLogs = eleLog
              } else {
                this.eleLogs = this.eleLogs.concat(eleLog)
              }
              if(data.data.data.totalPage === 0){

                this.allLoaded = true;// 若数据已全部获取完毕
              }
              if (this.pageNo === data.data.data.totalPage) {
                this.allLoaded = true;// 若数据已全部获取完毕
              } else {
                cb && cb()
              }
            }).catch((err) => {
              this.$toast(err.data.message)
            })
          },
          loadBottom() {

            this.getEleFaultHis(() => {
              this.$refs.loadmore.onBottomLoaded();
              this.pageNo++
            })
          }

        }
      })
    }
  }
  page._init()
}